namespace FIREFLYX {
    /**
     * （shadowMap）参考链接：http://webglsamples.org/WebGL2Samples/#fbo_rtt_depth_texture
     * 
     */
    /** 后处理效果  深度纹理 */
    export class PostEffectDepthTex implements IExample {
        public Init(canvas: HTMLCanvasElement): void {
            console.log("PostEffectDepthTex");
            let gl = canvas.getContext("webgl2", { antialias: false });
            //app init
            App.init(gl.canvas as any);
            //简单观察相机
            let cVEnv = GenCamViewEnv();
            let cam = cVEnv.camera;
            //添加 后处理效果 组件
            let depthTexDisplayComp = cam.entity.addComponent<DepthTexDisplay>(DepthTexDisplay);
            //灯光
            const light = GenMainLight(45);

            //模型节点
            let model = GameObjectUtil.createPrimitive(GameObjectPrimitiveType.CYLINDER);
            model.transform.scale = new Vector3(2, 2, 2);
        }
    }

    /** 实现 DepthTexDisplay 效果的脚本组件 */
    class DepthTexDisplay extends Script {
        private _mtr: Material;

        private makeShader() {
            //后处理 顶点程序
            let postEffVS = `#version 300 es
                precision highp float;
                precision highp int;
                layout (location = 0) in vec4 a_Position; //顶点 位置 是有默认值的 (0,0,0,1)
                layout (location = 3) in vec2 a_UV;       //UV 纹理坐标
                out vec2 v_uv;
                void main() {
                    v_uv = a_UV;
                    vec4 pos = vec4(a_Position.xyz , 1.0);
                    gl_Position = pos;
                }
            `;

            //后处理 片源程序
            let postEffFS = `#version 300 es
                precision highp float;
                precision highp int;
                uniform sampler2D u_mainTex;
                uniform sampler2D u_cameraDepthTexture;
                in vec2 v_uv;
                out vec4 color;
                void main(){
                    vec4 texColor = texture(u_cameraDepthTexture , v_uv);    //深度纹理
                    vec3 Depth = vec3(1.0 - texColor.r) * 500.;   //500 缩放系数是为了方便观察
                    color = vec4(Depth , 1.0);
                }
            `;

            //glsl
            const vs = new GLSL();
            vs.setCode(postEffVS);
            const fs = new GLSL();
            fs.setCode(postEffFS);

            //shader
            let result: Shader = new Shader();
            result.lightMode = RenderPipeline.LM_FORWARD;
            result.vertexGLSL = vs;
            result.fragmentGLSL = fs;
            //unifrom 默认值
            result.definePropertyMap = {};

            result.definePropertyMap.u_mainTex = {
                type: "texture",
                value: "White",
            };
            result.definePropertyMap.u_cameraDepthTexture = {
                type: "texture",
                value: "White",
            };

            //apply
            result.apply();

            return result;
        }

        public start() {
            let sh = this.makeShader();
            this._mtr = new Material("DepthTexDisplay");
            this._mtr.shader = sh;
        }

        public onRenderImage(src: RenderTexture2D, dest: RenderTexture2D) {
            //渲染
            const mtr = this._mtr;
            Graphics.Blit(src, dest, mtr);  //直接从src 绘制到 dest
        }
    }
}